package pt.iul.poo.file;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.LinkedList;

import pt.iul.poo.balloons.Balloon;
import pt.iul.poo.balloons.BlackBalloon;
import pt.iul.poo.balloons.BlueBalloon;
import pt.iul.poo.balloons.GreenBalloon;
import pt.iul.poo.balloons.RedBalloon;

public class CharacterHandlerBallons {

	private Charset asciiCodification = Charset.defaultCharset();
	private LinkedList<Balloon> listElementsFromFile = new LinkedList<Balloon>();
	private Dimension imagesSize;
	private Dimension startPosition;
	public LinkedList<Balloon> readFromFile(String fileName,
			Dimension startPosition) throws Exception {
		this.setStartPosition(startPosition);
		this.setStartPosition(startPosition);
		try {
			InputStream in = new FileInputStream(fileName);
			Reader reader = new InputStreamReader(in, asciiCodification);
			Reader buffer = new BufferedReader(reader);
			handleCharacters(buffer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return getListElementsFromFile();

	}

	private void handleCharacters(Reader reader) throws IOException {
		int r;
		while ((r = reader.read()) != -1) {
			char ch = (char) r;
			putInList(ch);
			System.out.println("Do something with " + ch);
		}
	}

	private void putInList(char caracterChar) throws IllegalArgumentException,
			IOException {
		String caracterString = "" + caracterChar;
		Balloon balloonTemp = null;
		switch (caracterString) {
		case "B":
			balloonTemp = new BlackBalloon("BlackBalloon.jpg",
					getStartPosition().width, getStartPosition().height);
			break;
		case "A":
			balloonTemp = new BlueBalloon("BlueBalloon.jpg",
					getStartPosition().width, getStartPosition().height);
			break;
		case "G":
			balloonTemp = new GreenBalloon("GreenBalloon.jpg",
					getStartPosition().width, getStartPosition().height);
			break;
		case "R":
			balloonTemp = new RedBalloon("RedBalloon.gif",
					getStartPosition().width, getStartPosition().height);
			break;
		default:
			break;
		}
		if (balloonTemp != null) {
			getListElementsFromFile().add(balloonTemp);
		}
	}

	public LinkedList<Balloon> getListElementsFromFile() {
		return listElementsFromFile;
	}

	public void setListElementsFromFile(LinkedList<Balloon> listElementsFromFile) {
		this.listElementsFromFile = listElementsFromFile;
	}

	public Dimension getImagesSize() {
		return imagesSize;
	}

	public Dimension getStartPosition() {
		return startPosition;
	}

	public void setStartPosition(Dimension startPosition) {
		this.startPosition = startPosition;
	}

}